package com.dlc.felear.xiaoensale.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.dao.OperatorDao;
import com.dlc.felear.xiaoensale.entity.CookingMenu;
import com.dlc.felear.xiaoensale.entity.Operator;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.dlc.felear.xiaoensale.utils.MenuPicUtil;

import java.util.ArrayList;
import java.util.List;


public class EmnuListtemAdapter1 extends BaseAdapter {

    private static final String TAG = "EmnuListtemAdapter";
    // 上下文
    private Context context;
    // 数据
    private List<CookingMenu> items;
    private OperatorDao operatorDao;

    public EmnuListtemAdapter1(Context context) {
        this.context = context;
        items = new ArrayList<>();
        operatorDao = new OperatorDao(context);
    }

    public List<CookingMenu> getData() {
        return items;
    }


//    public void setItems(List<CookingMenu> data) {
//        if (data==null||data.isEmpty()) {
//            notifyDataSetChanged();
//            return;
//        }
//        items.clear();
//        items.addAll(data);
//        notifyDataSetChanged();
//    }

    public void clearData() {
        items.clear();
        notifyDataSetChanged();
    }

    public void setItems(List<CookingMenu> data, String type) {
        try {
            if (data == null || data.isEmpty()) {
                notifyDataSetChanged();
                return;
            }
            items.clear();
            List<CookingMenu> templist = new ArrayList<>();
            for (CookingMenu cookingMenu : data) {
                Log.e(TAG, "type:" + type + " cookingMenu:" + JsonHelper.toJson(cookingMenu));
                if (TextUtils.equals(type, cookingMenu.getMachineModoel())) {
                    templist.add(cookingMenu);
                }
            }
            items.addAll(templist);
            notifyDataSetChanged();
        } catch (Exception e) {
            Log.e(TAG, "setItems:" + e.getMessage());
        }
    }


    @Override
    public int getCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public CookingMenu getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.adapter_menu_list_item1, null);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.tv_type = convertView.findViewById(R.id.tv_type);
            holder.iv_icon = convertView.findViewById(R.id.iv_icon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String name = items.get(position).getName() + "(" + items.get(position).getSpecification() + "Kg)";
        holder.tv_name.setText(name);
        holder.tv_type.setText("类型:" + items.get(position).getMachineModoel());
        String pic = items.get(position).getPic();
//        Log.e(TAG, "pic=" + pic);
        if (TextUtils.isEmpty(pic)) {
            Glide.with(context).load(R.drawable.menu_icon).into(holder.iv_icon);
        } else {
            Log.e(TAG, "pic=" + pic);
            Glide.with(context).load(MenuPicUtil.getInstance(context).getLoaclpicPath(pic,items.get(position).getName())).into(holder.iv_icon);
        }
        return convertView;
    }

    static class ViewHolder {
        TextView tv_name, tv_type;
        ImageView iv_icon;
    }

}